<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            top: 0;
            left: 0;
        }

        .box2 {
            width: 400px;
            height: 400px;
            border: 2px solid;
            margin-top: 100px;
        }
    </style>
</head>

<body>
    <div class="box1" draggable="true"></div>
    <div class="box2"></div>



    <script>

        // 拖放：拖拽（drag） 释放（drop）
        //选中---->拖动---->释放

        let box1 = document.getElementsByClassName('box1')[0];
        let box2 = document.getElementsByClassName('box2')[0];
        box1.ondragstart = function (event) {
            console.log('start');
            box2.ondragover = function (e) {
            e.preventDefault();
            console.log('over');
            }
            box2.ondrop = function (e) {
                console.log('drop');
                box1.style.left = e.clientX - event.offsetX + 'px';
                box1.style.top = e.clientY - event.offsetY + 'px';
            }
        }
        box1.ondrag = function () {
            console.log('drag');
        }

        box1.ondragend = function () {
            console.log('end');
        }


        // box2.ondragenter = function () {
        //     console.log('enter');
        // }
     
        // box2.ondragleave = function () {
        //     console.log('leave');
        // }


    </script>


</body>

</html>